{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 循环的技巧"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "在字典循环时， 使用items()方法可以同时取出键和对应的值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gallahad the pure\n",
      "robin the brave\n"
     ]
    }
   ],
   "source": [
    "knights = {'gallahad': 'the pure', 'robin': 'the brave'}\n",
    "for k, v in knights.items():\n",
    "    print(k, v)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "在序列循环时，用enumerate()函数可以同时取出位置索引和对应的值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 tic\n",
      "1 tac\n",
      "2 toe\n"
     ]
    }
   ],
   "source": [
    "for i, v in enumerate(['tic', 'tac', 'toe']):\n",
    "    print(i, v)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "同时循环两个或多个序列时，用zip()函数可以将其中的元素一一匹配"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "what is your name? It is lancelot\n",
      "what is your quest? It is the holy grail\n",
      "what is your favorite color? It is red\n"
     ]
    }
   ],
   "source": [
    "questions = ['name', 'quest', 'favorite color']\n",
    "answers = ['lancelot', 'the holy grail', 'red', 'blue']\n",
    "for q, a in zip(questions, answers):\n",
    "    print(f'what is your {q}? It is {a}')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "为了逆向对序列进行循环，可以先求出欲循环的正向序列，然后调用reversed()函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "7\n",
      "5\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "for i in reversed(range(1, 10, 2)):\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "按指定顺序循环序列，可以用sorted()函数，在不改动原序列的基础上，返回一个新的序列"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "apple\n",
      "banana\n",
      "orange\n",
      "orange\n",
      "pear\n"
     ]
    }
   ],
   "source": [
    "basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
    "for i in sorted(basket):\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用set()去重序列中重复的元素。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "orange\n",
      "pear\n"
     ]
    }
   ],
   "source": [
    "basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
    "for i in sorted(set(basket)):\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "在循环中修改列表的内容时，创建新列表比较简单，且安全"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[56.2, nan, 51.7, 55.3, 52.5, nan, 47.8]\n",
      "[56.2, 51.7, 55.3, 52.5, 47.8]\n",
      "[56.2, nan, 51.7, 55.3, 52.5, nan, 47.8]\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]\n",
    "print(raw_data)\n",
    "filtered_data = []\n",
    "for value in raw_data:\n",
    "    if not math.isnan(value):\n",
    "        filtered_data.append(value)\n",
    "\n",
    "print(filtered_data)\n",
    "print(raw_data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}